<!--机构奖金数据 机构-奖惩结果 -->
<template>
    <basic-container class="bonus">
      <flexible :leftCols="leftCols" :removeHeight="100" ref="flexibleRef" v-show="show">
        <template slot="left">
          <div class="search-form">
            <el-button type="primary"  @click="asyncAssessData" >同步奖惩</el-button>
            <el-button type="primary"  @click="setTransformRatio" >配置转换系数</el-button>
          </div>
          <!--左侧机构树组件-->
          <org-tree-by-dept ref="orgTreeByDept" @sendCurrentOrg="getCurrentOrg" :height="290"></org-tree-by-dept>
        </template>
        <template slot="right">
          <div class="search-form">
            <el-form :inline="true" :model="searchForm" >
              <el-form-item label="奖惩事项">
                <el-input v-model="searchForm.event" placeholder="奖惩事项" clearable></el-input>
              </el-form-item>
              <el-form-item label="分配状态">
                <el-select v-model="searchForm.assignmentStatus"  :popper-append-to-body="false">
                    <el-option value="已分配">已分配</el-option>
                    <el-option value="未分配">未分配</el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <div>
              <el-button type="primary" @click="handleSearch" :disabled="currentOrg == ''">查询</el-button>
            </div>
          </div>
          <div class="mb10">
            <el-button type="primary" @click="add" :disabled="currentOrg == ''">新增</el-button>
            <el-button type="danger" plain @click="del(true)" :disabled="currentOrg == ''">删除</el-button>
            <el-button type="primary" plain :disabled="currentOrg == ''" @click="downLoad">下载</el-button>
            <el-upload
              action='/jiangjin/jiangjinjjxxxb/importExcelFromTemplate'
              accept=".xlsx, .xls"
              :headers="headers"
              class="upload-demo inline-block"
              :before-upload="beforeAvatarUpload"
              :on-success="handleUploadSuccess"
              :on-error="handleUploadError"
              :show-file-list="false"
              :data="importData">
              <el-button size="small" class="ml10" type="primary" plain :disabled="currentOrg == ''">导入</el-button>
            </el-upload>
          </div>
          <el-table
            v-loading="tableLoading"
            :data="tableData"
            @selection-change="handleSelectionChange"
            height="calc(100vh - 300px)"
            border
            style="width: 100%">
            <el-table-column
              type="selection"
              align="center"
              width="55">
            </el-table-column>
            <el-table-column width="55" label="序号" align="center" header-align="center">
              <template slot-scope="scope">
                {{(page.currentPage - 1) * page.pageSize + scope.$index + 1}}
              </template>
            </el-table-column>

            <el-table-column
              prop="deptName"
              label="机构名称"
              show-overflow-tooltip
              min-width="150">
            </el-table-column>
            <el-table-column
              prop="deptCode"
              label="机构编号"
              show-overflow-tooltip
              min-width="130">
            </el-table-column>
            <el-table-column
              prop="bonusItemName"
              min-width="180"
              header-align="center"
              align="left"
              show-overflow-tooltip
              label="奖金项">
            </el-table-column>
            <el-table-column
              prop="bonusItemCode"
              width="140"
              show-overflow-tooltip
              align="center"
              header-align="center"
              label="奖金项编码(内)">
            </el-table-column>
            <el-table-column
              prop="event"
              min-width="150"
              header-align="center"
              align="left"
              show-overflow-tooltip
              label="奖惩事项">
            </el-table-column>
            <el-table-column
              prop="amount"
              min-width="140"
              header-align="center"
              align="center"
              show-overflow-tooltip
              label="奖惩金额（元）">
              <template slot-scope="scope">
                  <span :class="scope.row.amount < 0 ? 'red' : 'green'">{{scope.row.amount}}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="actDate"
              width="110"
              header-align="center"
              align="center"
              show-overflow-tooltip
              label="奖惩时间">
            </el-table-column>
            <el-table-column
              prop="reason"
              width="110"
              header-align="center"
              align="center"
              show-overflow-tooltip
              v-if="false"
              label="奖惩事由">
            </el-table-column>
            <el-table-column
              prop="assignmentStatus"
              width="90"
              header-align="center"
              align="center"
              show-overflow-tooltip
              label="状态">
              <template slot-scope="scope">
                <span :class="scope.row.assignmentStatus == '已分配' ? 'green' : 'red' ">{{scope.row.assignmentStatus}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="enableStatus" label="启用状态" min-width="140" align="center">
              <template slot-scope="scope">
                <el-switch
                  @change="handleSaveRowEdit(scope.row,scope.row.enableStatus)"
                  v-model="scope.row.enableStatus"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  active-text="启用"
                  inactive-text="停用"
                  :disabled="scope.row.enableStatus=='已使用'"
                  :active-value="0"
                  :inactive-value="1">
                </el-switch>
              </template>
            </el-table-column>
            <el-table-column align="center" width="180px" label="操作" fixed="right">
              <template slot-scope="scope">
                <el-button type="text" @click="handleRow('edit',scope.row)">编辑</el-button>
                <el-button type="text" @click="handleRow('view',scope.row)">查看</el-button>
                <el-button type="text" @click="distributeHandle(scope.row)">分配</el-button>
                <el-button type="text"  class="delete_btn" @click="del(false,scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!--分页-->
          <div class="avue-crud__pagination">
            <el-pagination
              @size-change="sizeChangeHandle"
              @current-change="currentChangeHandle"
              :current-page="page.currentPage"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="page.pageSize"
              :total="page.total"
              background
              layout="total, sizes, prev, pager, next, jumper"
            >
            </el-pagination>
          </div>
        </template>
      </flexible>

      <!--分配-->
      <distribute ref="distributeRef" v-show="!show" @close="close"></distribute>

      <!--添加/修改-->
      <rewardpunish-form ref="assessmentFormRef" @refresh="refreshTable" ></rewardpunish-form>

      <!--同步奖惩数据-->
      <assess-data-form ref="assessDataFormRef"></assess-data-form>
      <!-- 配置系数-->
      <set-transform-ratio-form ref="setTransformRatioFormRef"></set-transform-ratio-form>

    </basic-container>
</template>

<script>
import flexible from "@/views/bonusmanage/components/flexible";
import rewardpunishForm from "@/views/bonusmanage/data/org/rewardpunish/rewardpunishForm";
import {fetchList,delBatch,putObj} from '@/api/bonus/dataorgrewardpunish'
import orgTreeByDept from "@/views/bonusmanage/components/orgTreeByDept";
import distribute from "@/views/bonusmanage/data/org/rewardpunish/distribute";
import assessDataForm from "@/views/bonusmanage/components/asyncRewardpunishDataForm"
import setTransformRatioForm from "@/views/bonusmanage/components/setTransformRatioForm";
import store from "@/store";
export default {
  name: "index",
  components:{
    distribute,
    orgTreeByDept,
    rewardpunishForm,
    flexible,
    assessDataForm,
    setTransformRatioForm
  },
  watch: {

  },
  computed: {

  },
  data(){
    return{
      show:true,
      treeLoading:false,
      tableLoading:false,
      leftCols:6,
      searchForm: {},
      multipleSelection:[],
      page:{
        total: 0,
        currentPage: 1,
        pageSize: 10
      },
      tableData: [],
      currentOrg:'',
      //---------------导入导出-------------
      headers: {
        Authorization: "Bearer " + store.getters.access_token,
      },
      importData: {
        jjDeptId: undefined
      }
    }
  },
  created(){
    this.$nextTick(()=>{
      this.$refs.orgTreeByDept.getJiangJinJg() ;
    })
  },
  mounted() {
  },
  methods: {
    handleSelectionChange(list){
      this.multipleSelection = list ;
      this.ids = this.multipleSelection.map((ele) => {
        return ele.id
      })
    },
    refreshTable(){
      this.onLoad() ;
    },
    getCurrentOrg(item){
      this.currentOrg = item //当前点击的机构
      this.onLoad() ;
    },
    add(){
      if (JSON.stringify(this.currentOrg) == '{}'){
        this.$message.error('请选择机构')
        return
      }
      this.$refs.assessmentFormRef.init('add',{},this.currentOrg)
    },
    //删除
    del(batch,item){
      let msg = '' ;
      let ids = ''
      if (batch){ //批量
        msg = '确定删除选中的数据吗?'
        if (this.multipleSelection.length == 0){
          this.$message.error('请勾选要删除的数据')
          return ;
        }
        ids = this.ids.toString() ;
      }else{ //单条
        msg = '确定删除此条数据吗?'
        ids = item.id ;
      }
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.treeLoading = true ;
        delBatch({ids:ids}).then(res=>{
          this.$message.success('操作成功')
          this.onLoad();
        }).catch(()=>{
          this.tableLoading = false ;
        })
      }).catch(() => {

      });
    },
    //编辑、查看
    handleRow(type,item){
      this.$refs.assessmentFormRef.init(type,JSON.parse(JSON.stringify(item)))
    },
    //分配
    distributeHandle(item){
      this.show = false ;
      this.$nextTick(()=>{
        this.$refs.distributeRef.init(item) ;
      })
    },
    handleSearch(){
      this.page.currentPage = 1;
      this.onLoad() ;
    },
    onLoad(){
      this.tableLoading = true ;
      fetchList(Object.assign({
        current: this.page.currentPage,
        size: this.page.pageSize
      },{deptBaseId:this.currentOrg.id},this.searchForm)).then(res=>{
        if (res.data.code == 0){
          this.tableData = res.data.data.records
          this.page.total = res.data.data.total ;
          this.page.currentPage = res.data.data.current ;
        }else{
          this.$message.error(res.data.msg)
        }
        this.tableLoading = false ;
      }).catch(()=>{
        this.tableLoading = false ;
      })
    },

    // 每页数
    sizeChangeHandle(val) {
      this.page.pageSize = val;
      this.page.currentPage = 1;
      this.onLoad() ;
    },
    // 当前页
    currentChangeHandle(val) {
      this.page.currentPage = val;
      this.onLoad() ;
    },
    close(refresh){
      this.show = true ;
      if (refresh){
        this.onLoad() ;
      }
    },
    exportData(){
      alert("导入功能待开发")
    },
    asyncAssessData(){
      this.$refs.assessDataFormRef.init('jg') ;
    },
    setTransformRatio(){
      this.$refs.setTransformRatioFormRef.init('jg',  this.$refs.orgTreeByDept.deptId) ;
    },
    //保存行编辑
    handleSaveRowEdit(row) {
      putObj(row).then(res => {
        if (res.data.code == 0){
          row.enableStatus == 1 ? this.$message.success('停用成功') : this.$message.success('启用成功')
        }else{ //修改失败后
          row.enableStatus == 1 ? row.enableStatus = 0 : row.enableStatus = 1
          row.enableStatus == 1 ? this.$message.error('停用失败') : this.$message.error('启用失败')
        }
      }).catch(() => {
          row.enableStatus == 1 ? row.enableStatus = 0 : row.enableStatus = 1
      });
    },
    //--------------------导入导出------------
    downLoad(){

    },
    /**
     * 文件上传前
     */
    beforeAvatarUpload(file) {
      this.loading = this.$loading({
        lock: true,
        text: '文件上传中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
    },
    /**
     * 文件上传成功到pigx的统计文件管理后转发
     */
    handleUploadSuccess(response, file, fileList) {
      this.loading.close()
      this.getDataList();
      if (response.data) {//如果解析数据中由特殊情况，向前台返回1，有错误数据返回2
        if (response.data == 2) {
          this.$message.error(response.msg)
        } else {
          this.$message.warning(response.msg)
        }
      } else {
        this.$message.success(response.msg)
      }
    },
    handleUploadError(response, file, fileList) {
      this.loading.close()
      this.$message.error('上传出错：' + response.message)
    },
  }
}
</script>

<style scoped>

</style>

